#include <stdio.h>
#include "Course.h"
#include <time.h>
#include "../ModuleC/Log.h"

void ReadCImformation(){//通过文件录入课程信息
	if (headC != NULL) {
		printf("课程信息已录入，请不要反复录入！\n");
		return ;
	}
	int i;
	FILE *fp;
	char filename[20];
	struct Course *p1;
	printf("请输入文件路径及文件名：\n");
	scanf("%s", filename);
	if ((fp = fopen(filename, "r")) == NULL) {
        	printf("打开文件失败！\n"); 
		return ; 
    	}
	p1 = (struct Course *)malloc(sizeof(struct Course));
	fscanf(fp, "%s %s %s", p1->CNO, p1->CN, p1->CP);
	fscanf(fp, "%lf %lf %d %d", &p1->CT, &p1->CS, &p1->ASN, &p1->LSN);
	for(i = 0; i < p1->ASN; i++)
		fscanf(fp, "%s", p1->CSN[i]);
	headC = p1;
	p1->next = NULL;
	while (!feof(fp)) { 
		p1 = (struct Course *)malloc(sizeof(struct Course));
		fscanf(fp, "%s %s %s", p1->CNO, p1->CN, p1->CP);
		fscanf(fp, "%lf %lf %d %d", &p1->CT, &p1->CS, &p1->ASN, &p1->LSN);
		for(i = 0; i < p1->ASN; i++)
			fscanf(fp, "%s", p1->CSN[i]);
		fscanf(fp, "\n");
		p1->next = headC->next;
		headC->next = p1;
	}   
	printf("录入课程信息成功！\n");
	fclose(fp);
	FILE *fp1;	
	fp1=fopen("FILE/log.log","a");
	Log(fp1);
	fprintf(fp1,"通过读取文件,录入了课程信息，调用了ReadCImformation()。\n");
	fclose(fp1);
}
